<template>
  <div class="container">
    <div class="app-container">
      <el-button type="primary" @click="addPermission(0,1)">添加权限</el-button>
      <el-table
        :data="tableData"
        row-key="id"
      >
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button v-if="row.type ===1" type="text" @click="addPermission( row.id,2)">添加</el-button>
            <el-button type="text" @click="UpdatePermission(row.id)">编辑</el-button>
            <el-popconfirm
              title="这是一段内容确定删除吗？"
              @confirm="delPermission(row.id)"
            >
              <el-button slot="reference" type="text" style="margin-left:10px">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 弹窗 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      @close="close"
    >
      <el-form ref="formRef" label-width="100px" :model="datalList" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="datalList.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="datalList.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="datalList.description" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="datalList.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { reqGetgetPermissionList, reqAddPermission, reqUpdatePermission, reqGetPermission, reqDelPermission } from '@/api/permission'
import { translatetoTreeDate } from '../../utils'
export default {
  name: 'Permission',
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      title: '新增权限点',
      datalList: {
        name: '',
        code: '',
        description: '',
        enVisible: 0,
        type: '',
        pid: ''
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trrgger: 'change' }],
        code: [{ required: true, message: '权限名称不能为空', trrgger: 'change' }]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const res = await reqGetgetPermissionList()
      console.log(res)
      this.tableData = translatetoTreeDate(res.data, 0)
    },
    close() {
      this.dialogVisible = false
      // 重置
      this.$refs.formRef.resetFields()
      this.datalList = {
        name: '',
        code: '',
        description: '',
        enVisible: 0,
        type: '',
        pid: ''
      }
    },
    // 点击显示弹框
    addPermission(pid, type) {
      this.dialogVisible = true
      this.datalList.type = type
      this.datalList.pid = pid
    },
    // 添加
    handleSubmit() {
      this.$refs.formRef.validate(async too => {
        if (too) {
          if (this.datalList.id) {
            this.title = '编辑权限点'
            await reqUpdatePermission(this.datalList)
          } else {
            this.title = '新增权限点'
            await reqAddPermission(this.datalList)
          }
          this.close()
          this.getPermissionList()
        }
      })
    },
    // 数据回显
    async UpdatePermission(id) {
      this.dialogVisible = true
      const res = await reqGetPermission(id)
      // console.log(res)
      this.datalList = res.data
    },
    // 删除
    async delPermission(id) {
      await reqDelPermission(id)
      this.getPermissionList()
    }

  }
}
</script>
